package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.WorkTeam;
import com.mxpio.erp.common.technology.entity.WorkTeamMember;
import com.mxpio.erp.common.technology.service.WorkTeamMemberService;
import com.mxpio.erp.common.technology.service.WorkTeamService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "StandTecRoutController", description = "班组信息接口")
@RestController
@RequestMapping("/erp/tech/workteam/")
public class WorkTeamController {

    @Autowired
    private WorkTeamService service;

    @Autowired
    private WorkTeamMemberService lineService;

    @GetMapping("list")
    @Operation(summary = "班组信息列表", description = "获取班组信息列表（无分页）", method = "GET")
    public Result<List<WorkTeam>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<WorkTeam> list = service.list(WorkTeam.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "班组信息列表", description = "获取班组信息列表（分页）", method = "GET")
    public Result<Page<WorkTeam>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<WorkTeam> listPage = service.listPage(WorkTeam.class, page, criteria);
        return Result.OK(listPage);
    }

    @GetMapping("list/{workTeamCode}")
    @Operation(summary = "根据workTeamCode获取班组信息", description = "根据workTeamCode获取班组信息", method = "GET")
    public Result<WorkTeam> getById(@Parameter(description="通知单号") @PathVariable(name = "workTeamCode", required = true) String workTeamCode) {
        //带明细行数据
        WorkTeam data = service.getById(WorkTeam.class,workTeamCode);
        return Result.OK(data);
    }

    @DeleteMapping("remove/{workTeamCodes}")
    @Operation(summary = "根据workTeamCode删除班组信息", description = "删除班组信息", method = "DELETE")
    public Result<WorkTeam> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "workTeamCodes", required = true) String workTeamCodes) {
        String[] workTeamCode = workTeamCodes.split(",");
        for(String key : workTeamCode){
            Criteria criteria = Criteria.create();
            criteria.addCriterion("workTeamCode", Operator.EQ,key);
            lineService.removeBatch(WorkTeamMember.class,criteria);
            service.delete(WorkTeam.class, key);
        }
        return Result.OK();
    }

    @RequestMapping("save")
    @Operation(summary = "保存班组信息", description = "保存班组信息")
    public Result<WorkTeam> save(@RequestBody WorkTeam team) {
        service.save(team,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if(o instanceof WorkTeamMember){
                    WorkTeam parent = context.getParent();
                    ((WorkTeamMember) o).setWorkTeamCode(parent.getWorkTeamCode());
                }
                return true;
            }
        });
        return Result.OK(team);
    }

    @GetMapping("current")
    @Operation(summary = "当前登录人所属班组信息", description = "当前登录人所属班组信息", method = "GET")
    public Result<WorkTeam> currentUser() {
        WorkTeam data = service.getByCurrentUser();
        return Result.OK(data);
    }
}
